package com.example.demo.configuration;

import com.example.demo.support.DebugInterceptor;
import org.aopalliance.aop.Advice;
import org.springframework.aop.Pointcut;
import org.springframework.aop.support.AbstractPointcutAdvisor;
import org.springframework.aop.support.JdkRegexpMethodPointcut;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Configuration
public class TestConfiguration extends AbstractPointcutAdvisor implements BeanFactoryAware {

    private BeanFactory beanFactory;

    private Advice advice;

    private Pointcut pointcut;

    @PostConstruct
    public void init() {
        System.out.println("===========PostConstruct");
        this.advice = new DebugInterceptor();
        JdkRegexpMethodPointcut jdkRegexpMethodPointcut = new JdkRegexpMethodPointcut();
        jdkRegexpMethodPointcut.setPattern(".*sout");
        this.pointcut = jdkRegexpMethodPointcut;
    }

    @PreDestroy
    public void destroy() {
        System.out.println("===========PreDestroy");
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
    }

    @Override
    public Pointcut getPointcut() {
        return this.pointcut;
    }

    @Override
    public Advice getAdvice() {

        return this.advice;
    }
}
